package com.ss.android.article.base.feature.subscribe.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.subscribe.b.SubscribeManager;
import com.ss.android.article.base.feature.subscribe.b.SubscribeManager.SubscribeDataChangeListener;
import com.ss.android.article.base.feature.subscribe.model.EntryGroup;
import com.ss.android.article.base.feature.subscribe.model.SubscribeData;
import com.ss.android.article.browser.R;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.newmedia.activity.FragmentActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class EntryGroupListActivity extends FragmentActivity implements SubscribeDataChangeListener {
    final class EntryGroupListActivity$a extends BaseAdapter {
        final class EntryGroupListActivity$a$a {
            public TextView a;
            public View b;
            public ImageView c;

            EntryGroupListActivity$a$a(byte arg1) {
                this();
            }

            private EntryGroupListActivity$a$a() {

            }
        }

        private List<EntryGroup> a;

        public EntryGroupListActivity$a(Collection arg2) {
            this.a = new ArrayList();
            this.a.addAll(arg2);
        }

        public final int getCount() {
            return this.a.size();
        }

        public final Object getItem(int arg2) {
            return this.a.get(arg2);
        }

        public final long getItemId(int arg3) {
            return ((long) arg3);
        }

        public final View getView(int arg5, View arg6, ViewGroup arg7) {
            if (arg6 == null) {
                arg6 = LayoutInflater.from(arg7.getContext()).inflate(com.ss.android.article.browser.R.layout.entry_group_item, arg7, false);
                EntryGroupListActivity$a$a v1 = new EntryGroupListActivity$a$a((byte) 0);
                v1.a = arg6.findViewById(com.ss.android.article.browser.R.id.text);
                v1.b = arg6.findViewById(com.ss.android.article.browser.R.id.container);
                v1.c = arg6.findViewById(com.ss.android.article.browser.R.id.entry_group_indicator);
                arg6.setTag(v1);
            }

            Object v0 = arg6.getTag();
            ((EntryGroupListActivity$a$a) v0).a.setText(this.a.get(arg5).name);
            AppData.getInstance();
            AppData.isNightMode();
            ((EntryGroupListActivity$a$a) v0).b.setBackgroundResource(com.ss.android.article.browser.R.drawable.bg_entry_group_item);
            ((EntryGroupListActivity$a$a) v0).a.setTextColor(((EntryGroupListActivity$a$a) v0).b.getContext().getResources().getColorStateList(com.ss.android.article.browser.R.color.entry_group_text));
            ((EntryGroupListActivity$a$a) v0).c.setBackgroundResource(com.ss.android.article.browser.R.drawable.entry_group_list_item_indicator);
            return arg6;
        }
    }

    EntryGroup a;
    private ListView b;
    private long c;
    private List<EntryGroup> d;
    private View e;
    private View f;
    private int g;
    private String h;
    private JSONObject i;

    public EntryGroupListActivity() {

    }

    final void a() {
        if (this.a != null) {
            c v0 = new c();
            Bundle v1 = new Bundle();
            v1.putLong("group_id", this.a.id);
            v0.setArguments(v1);
            this.getSupportFragmentManager().beginTransaction().replace(com.ss.android.article.browser.R.id.sh, ((Fragment) v0)).commit();
        }
    }

    public final void onSubscribeDataChanged(SubscribeData arg3) {
        if ((this.isActive()) && arg3.a == 2) {
            this.f.setVisibility(View.VISIBLE);
            this.e.setVisibility(View.GONE);
            this.b();
        }
    }

    private void b() {
        int v2 = 0;
        int v0 = this.b.getCheckedItemPosition();
        this.d = new ArrayList();
        SubscribeManager v1 = SubscribeManager.getInstance();
        List v3 = this.d;
        SubscribeManager.checkUIThread();
        com.ss.android.article.base.feature.subscribe.b.e v1_1 = v1.c;
        v3.clear();
        v3.addAll(v1_1.h);
        if (this.d == null || this.d.size() <= 0) {
            this.f.setVisibility(View.GONE);
        } else {
            this.b.setAdapter(new EntryGroupListActivity$a(this.d));
            if (v0 == -1 || v0 >= this.d.size()) {
                if (this.c > 0) {
                    int v1_2 = 0;
                    while (v1_2 < this.d.size()) {
                        if (this.d.get(v1_2).id == this.c) {
                            v2 = v1_2;
                        } else {
                            ++v1_2;
                            continue;
                        }

                        break;
                    }
                }

                this.b.setItemChecked(v2, true);
                this.a = this.d.get(v2);
            } else {
                this.b.setItemChecked(v0, true);
                this.a = this.d.get(v0);
            }

            this.a();
            this.b.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                public final void onItemClick(AdapterView arg4, View arg5, int arg6, long arg7) {
                    EntryGroupListActivity.this.a = (EntryGroup) arg4.getAdapter().getItem(arg6);
                    EntryGroupListActivity.this.a();
                    MobClickCombiner.onEvent(EntryGroupListActivity.this, "subscription", "change_cat");
                }
            });
        }
    }

    public int getDayBackgroundRes() {
        return R.color.activity_bg_color;
    }

    public int getLayout() {
        return R.layout.d0;
    }

    public int getNightBackgroundRes() {
        return R.color.activity_bg_color;
    }

    public void init() {
        int v7 = View.GONE;
        long v4 = 0;
        super.init();
        Intent v0 = this.getIntent();
        if (v0 != null) {
            this.g = v0.getIntExtra("list_type", -1);
            this.h = v0.getStringExtra("from_category");
            String v0_1 = v0.getStringExtra("gd_ext_json");
            if (!android.support.design.a.isTextEmpty(v0_1)) {
                try {
                    this.i = new JSONObject(v0_1);
                } catch (JSONException v0_2) {
                    Logger.e("EntryGroupListActivity", "exception in initData " + v0_2.toString());
                }
            }
        }
        this.mTitleView.setText(this.getString(com.ss.android.article.browser.R.string.title_add_subscribe_page));
        this.mRightBtn.setVisibility(View.VISIBLE);
        this.mRightBtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.g0, 0);
        this.mRightBtn.setOnClickListener(new View.OnClickListener() {

            public final void onClick(View arg4) {
                EntryGroupListActivity entryGroupListActivity = EntryGroupListActivity.this;
                MobClickCombiner.onEvent(entryGroupListActivity, "subscription", "search");
                Intent intent = new Intent();
                intent.setClassName(entryGroupListActivity, "com.ss.android.article.base.feature.pgc.PgcSearchActivity");
                entryGroupListActivity.startActivity(intent);
            }
        });
        this.b = (ListView) this.findViewById(R.id.sg);
        this.e = this.findViewById(com.ss.android.article.browser.R.id.sf);
        this.f = this.findViewById(com.ss.android.article.browser.R.id.content_layout);
        SubscribeManager.init(((Context) this));
        SubscribeManager.getInstance().addSubscribeDataChangeListener(((SubscribeDataChangeListener) this));
        if (this.getIntent() != null) {
            this.c = this.getIntent().getLongExtra("group_id", v4);
        }

        this.b();
        this.f.setVisibility(v7);
        this.e.setVisibility(View.VISIBLE);
        SubscribeManager v0_3 = SubscribeManager.getInstance();
        SubscribeManager.checkUIThread();
        v0_3.c.a();
        String v2 = "add_entry";
        JSONObject v8 = this.i;
        String v3 = "";
        if (android.support.design.a.isTextEmpty(v3)) {
            if (this.g == 1) {
                if ("__all__".equals(this.h)) {
                    v3 = "click_headline";
                } else if (!android.support.design.a.isTextEmpty(this.h)) {
                    v3 = "click_" + this.h;
                }
            } else if (this.g == 3) {
                v3 = "click_search";
            } else if (this.g == 4) {
                v3 = "click_pgc_list";
            } else if (this.g == 2) {
                v3 = "click_favorite";
            } else if (this.g == v7) {
                v3 = "click_read_history";
            } else if (this.g == 9) {
                v3 = "click_push_history";
            } else if (this.g == 10) {
                v3 = "click_refresh_history";
            }
        }

        if (!android.support.design.a.isTextEmpty(v2)) {
            MobClickCombiner.onEvent(this, v2, v3, v4, v4, v8);
        }
    }

    public void onBackPressed() {
        if (!this.isFinishing()) {
            Intent v0 = null;
            if (this.isTaskRoot()) {
                v0 = ToolUtils.getLaunchIntentForPackage(((Context) this), this.getPackageName());
            }

            this.finish();
            if (v0 == null) {
                return;
            }

            this.startActivity(v0);
        }
    }

    public void onDestroy() {
        SubscribeManager.getInstance().b(((SubscribeDataChangeListener) this));
        super.onDestroy();
    }
}

